
module.exports = (secret) => {
    return async function jwt(ctx, next) {
        let token = ctx.request.header.authorization;
        !token && (token = ctx.request.query.authorization);  //如果请求头没有token，就在路径参数里面找是否有token
        if (token) {
            try {
                let decodeToken = ctx.app.jwt.verify(token, secret);
                await next();
            } catch (err) {
                // ctx.status = 401;
                // ctx.body = responseBody(-1, "token已失效，请重新登录！");
                ctx.body = { code: -1, msg: "token已失效，请重新登录！" };
            }
        } else {
            // ctx.status = 401;
            // ctx.body = responseBody(-2, "请求中未携带token！");
            ctx.body = { code: -2, msg: "请求中未携带token！" };
        }
    };
}